import org.junit.Test;

import java.io.*;

public class CharacterStream {
    public static void main(String[] args) {

    }

    @Test
    public void Test1() {
        File f1 = new File("J:\\JavaTestArea\\zzz.txt");
        System.out.println(f1.getAbsoluteFile());
        FileReader fr = null;
        try {
            fr = new FileReader(f1);
//        for (int j = 0; j < 12; j++) {
//            int i = fr.read();
//            System.out.println(i);
//        }
            int i = fr.read();
            while (i != -1) {
                System.out.print((char) i);
                i = fr.read();
            }
            System.out.println();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Test
    public void TestFileRead() {
        File f1 = new File("Hello.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(f1);
            char[] c = new char[5];
            int read;
            while ((read = fr.read(c)) != -1) {
                for (int i = 0; i < read; i++) {
                    System.out.print(c[i]);
                }
                String st = new String(c, 0, read);
                System.out.println(st);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void TestFileWriter() throws IOException {
        File f1 = new File("J:\\JavaTestArea\\zzz23.txt");
        FileWriter fw = new FileWriter(f1, true);
        fw.write("\nHello World!\n");
        fw.write("Hello World? No!");

        fw.close();

    }

    @Test
    public void TestWR()  {
        FileReader Fr = null;
        FileWriter Fw = null;
        try {
            File f1 = new File("J:\\JavaTestArea\\JTA1\\Source.TXT");
            File f2 = new File("J:\\JavaTestArea\\JTA1\\Goal.TXT");
            Fr = new FileReader(f1);
            Fw = new FileWriter(f2, true);
            char[] ch = new char[5];
            int length;
            String st;
            while ((length = Fr.read(ch)) != -1) {
                st = new String(ch, 0, length);
                System.out.println(st);
                Fw.write(st);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {if (Fr!=null)
                Fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (Fw!=null)
                Fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
